package deprecated.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import deprecated.hibernate.HibernateUtil;
import domain.dao.interfaces.IDAOContact;
import domain.dao.interfaces.IDAOContactGroup;
import util.DomainUtils;
import util.FormArgumentsException;

/**
 * Servlet implementation class ActionDispatcherServlet
 */
public class ActionDispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ActionDispatcherServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String selectedContact = request.getParameter("selected_contact");
		String wantedAction = request.getParameter("wanted_action");
		String[] contactGroups = request.getParameterValues("contactgroups");
		String keywords = DomainUtils.trim(request.getParameter("keywords"));
		int idContact = -1;
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {
			checks(request, selectedContact, wantedAction, contactGroups,keywords);
			request.setAttribute("selected_contact", selectedContact);
			idContact = Integer.parseInt(selectedContact);

			if(wantedAction.compareTo("show_details_contact") == 0) {
				request.getRequestDispatcher("pages/showContactDetails.jsp").forward(request, response);
			}
			else if(wantedAction.compareTo("modify_contact") == 0) {
				request.getRequestDispatcher("pages/updateContact.jsp").forward(request, response);
			}
			else if(wantedAction.compareTo("delete_contact") == 0) {
				IDAOContact daoContact = (IDAOContact) context.getBean("beanDAOContact");
				daoContact.deleteContact(idContact);
				//DAOFactory.getDaoContact().deleteContact(null, idContact);


				DomainUtils.displayInfoMessage(
						request, 
						DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR, 
						"The contact has been successfully deleted.");

				request.getRequestDispatcher("pages/showAllContact.jsp").forward(request, response);
			}
			else if(wantedAction.compareTo("add_to_contactgroup") == 0) {
				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				daoContactGroup.addContactToGroups(
						 contactGroups, idContact);
				//				DAOFactory.getDaoContactGroup().addContactToGroups(
				//						null, contactGroups, idContact);


				DomainUtils.displayInfoMessage(
						request, 
						DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR, 
						"The contact has been successfully added to the specified groups.");


				request.getRequestDispatcher("pages/showAllContact.jsp").forward(request, response);
			}
			else if(wantedAction.compareTo("remove_from_contactgroup") == 0) {

				IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
				daoContactGroup.removeContactFromGroups(
						 contactGroups, idContact);
				//				DAOFactory.getDaoContactGroup().removeContactsFromGroup(
				//						null, contactGroups, idContact);


				DomainUtils.displayInfoMessage(
						request, 
						DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR, 
						"The contact has been successfully deleted from the specified groups.");


				request.getRequestDispatcher("pages/showAllContact.jsp").forward(request, response);
			}
			else {
				throw new Exception();
			}
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}


			DomainUtils.keepParametersContent(request);
			request.getRequestDispatcher("pages/showAllContact.jsp").forward(request, response);
		}

	}

	private void checks(HttpServletRequest request, String selectedContact,
			String wantedAction, String[] contactGroups, String keywords) throws Exception {

		HttpSession session = request.getSession(false);

		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}

		
		//Checking condition for add to group
		if(wantedAction.compareTo("add_to_contactgroup") == 0) {
			if(contactGroups == null) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
						"You need to select at least one group.");
				throw new FormArgumentsException();
			}
		}

		
		//Checking condition for remove from group
		if(wantedAction.compareTo("remove_from_contactgroup") == 0) {
			if(contactGroups == null) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
						"You need to select at least one group.");
				throw new FormArgumentsException();
			}
		}
	}
}
